//返回首页
$('.back').click(function () {
    location.href = ('/')
})

//发送ajax获取新品页数据
promiseAjax({
    url: '/api/getMoreNewItem',
    method: 'post',
}).then(res => {
    res = JSON.parse(res)
    console.log(res);
    var newItems = res.data.newItems;
    console.log(newItems);
    if (res.code == 200) {
        var k = 0
        new Page('myPagination', {
            pageData: {
                total: newItems.length,
                pageSize: 8
            },
            show: function (currentPage) {
                var data1 = newItems.slice((currentPage - 1) * 8, currentPage * 8)
                $('.xinShow').html('')
                for (var i = 0; i < data1.length; i++) {
                    var div = $('<div class="xinShi" detail-id="' + data1[i].id + '"></div>')
                    var img = $('<img src="" alt="" index=' + k + '>')
                    img.attr('src', data1[i].listPicUrl)
                    var h4 = $('<h4 class="xinTitle"></h4>')
                    h4.text(data1[i].name)
                    var p1 = $('<p></p>')
                    var span1 = $('<span>￥</span>')
                    var span2 = $('<span class="retailPrice"></span>')
                    span2.text(data1[i].retailPrice)
                    var span3 = $('<span>￥</span>')
                    var span4 = $('<span class="counterPrice"></span>')
                    span4.text(data1[i].counterPrice)
                    var p2 = $('<p></p>')
                    var span5 = $('<span></span>')
                    span5.text(data1[i].simpleDesc)
                    p1.append(span1)
                    p1.append(span2)
                    p1.append(span3)
                    p1.append(span4)
                    div.append(img)
                    div.append(h4)
                    div.append(p1)
                    div.append(p2)
                    div.append(span5)
                    k++
                    $('.xinShow').append(div)
                }
                //移入是图片放大,且背景颜色改变
                $('.xinShi').hover(function () {
                    $(this).css('background-color', 'rgba(240,245,234,0.3)').siblings().css('background-color', 'white')
                    //移入改变图片路径
                    $(this).find('img').attr('src', newItems[$(this).find("img").attr("index")].primaryPicUrl)

                    //移入图片放大
                    $(this).find('img').css('transform', 'scale(1.1)').parent().siblings().find('img').css('transform', 'scale(1)')
                }, function () {
                    $(this).find('img').attr('src', newItems[$(this).find("img").attr("index")].listPicUrl)
                    $(this).css('background-color', 'white').siblings().css('background-color', 'white')
                })

                $('.xinShi').on('click', function () {
                    //获取当前点击商品的id
                    var goodsid = $(this).attr('detail-id')
                    console.log(goodsid);
                    location.href = '../detail.html?id='+goodsid

                })
            }
        })
    }
})

//发生ajax获取新品预告数据
promiseAjax({
    url: '/api/getPreNewItem',
    method: 'post',
}).then(res => {
    res = JSON.parse(res)
    console.log(res);
    var preNewItemList = res.data.preNewItemList;
    console.log(preNewItemList);
    if (res.code == 200) {
        var j = 0
        new Page('myPagination1', {
            pageData: {
                total: preNewItemList.length,
                pageSize: 4
            },
            show: function (currentPage) {
                var data = preNewItemList.slice((currentPage - 1) * 4, currentPage * 4)
                $('.xinShowPre').html('')
                for (var i = 0; i < data.length; i++) {
                    var div = $('<div class="xinShiPre" detail-id="' + data[i].id + '"></div>')
                    var img = $('<img src="" alt="" index=' + j + '>')
                    img.attr('src', data[i].listPicUrl)
                    var h4 = $('<h4 class="xinTitlePre"></h4>')
                    h4.text(data[i].name)
                    var p1 = $('<p></p>')
                    var span1 = $('<span>￥</span>')
                    var span2 = $('<span class="retailPrice"></span>')
                    span2.text(data[i].retailPrice)
                    var span3 = $('<span>￥</span>')
                    var span4 = $('<span class="counterPrice"></span>')
                    span4.text(data[i].counterPrice)
                    var p2 = $('<p></p>')
                    var span5 = $('<span></span>')
                    span5.text(data[i].simpleDesc)
                    var p3 = $('<p class="shangjia">即将上架</p>')
                    p1.append(span1)
                    p1.append(span2)
                    p1.append(span3)
                    p1.append(span4)
                    div.append(img)
                    div.append(h4)
                    div.append(p1)
                    div.append(p2)
                    div.append(span5)
                    div.append(p3)
                    j++
                    $('.xinShowPre').append(div)
                }
                $('.xinShiPre').hover(function () {
                    $(this).css('background-color', 'rgba(240,245,234,0.7)').siblings().css('background-color', 'white')
                    //移入改变图片路径
                    $(this).find('img').attr('src', preNewItemList[$(this).find("img").attr("index")].primaryPicUrl)

                    //移入图片放大
                    $(this).find('img').css('transform', 'scale(1.1)').parent().siblings().find('img').css('transform', 'scale(1)')
                }, function () {
                    $(this).find('img').attr('src', preNewItemList[$(this).find("img").attr("index")].listPicUrl)
                    $(this).css('background-color', 'white').siblings().css('background-color', 'white')
                })

                $('.xinShiPre').on('click', function () {
                    layer.msg('商品还未上架，请等待',{
                        icon:2,
                        time:1000
                    })
                })
            }
        })
    }
})